//
//  SetViewController.m
//  安好
//
//  Created by lianchuang on 14-11-6.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SetViewController.h"
#import "setFirstCellCell.h"
#import "setSecondCell.h"

#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //加载导航条
    [self makeNavBar];
    //加载数据
    [self loadData];
    //加载tableView
    [self createTableView];
    
}
/**
 *  创建数据
 */
-(void)loadData
{
    self.firstArray = @[@"日志",@"体检",@"挂号",@"自查"];
    self.secondArray = @[@"任务",@"百科",@"附近"];
}
#pragma mark - 配置导航条
-(void)makeNavBar
{
    self.navigationItem.title = @"设置";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    // 完成
    UIBarButtonItem *OkBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_完成.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC:)];
    OkBtnImg.tintColor = [UIColor whiteColor];
    OkBtnImg.tag = 100;
    self.navigationItem.rightBarButtonItem = OkBtnImg;
}
/**
 *  返回
 */
-(void)backMoreVC:(UIBarButtonItem*)sender
{
    //点击“完成” 右按钮 反向传值
    if (sender.tag == 100 )
    {
        self.switchStatesArray = [NSMutableArray arrayWithCapacity:0];
        [self.switchStatesArray addObject:@"1"];
        /**
         * 获取 switch的状态，存入switchStatesArray数组
         */
        for (int i = 0; i < self.secondArray.count; i++) {
            UISwitch *tempSwitch = (UISwitch *)[self.view viewWithTag:i+10];
            [self.switchStatesArray addObject:[NSString stringWithFormat:@"%d",tempSwitch.isOn]];
        }
        /**
         *  在pop之前 把数组保存到本地NSUserDefaults写入磁盘
         */
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:self.switchStatesArray forKey:@"MoreSetSwitch"];
        [user synchronize];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 创建tableView设置代理
-(void)createTableView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0 , kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //系统分割线
//    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.tableView];
    [self.tableView setSeparatorColor:[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0]];
}
#pragma mark - tableView的代理方法
/**
 *  设置 为 2 段
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.firstArray.count;
    }
    else
    {
        return self.secondArray.count;
    }
}
//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (IPhone6Plus) {
        return 54;
    }else{
        return 46;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IPhone6Plus) {
        return 53;
    }else{
        return 44;
    }
}
#pragma mark ----2中cell的加载
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        setFirstCellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"first"];
        if (cell == nil) {
            if (IPhone6Plus) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"setFirstCellCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"setFirstCellCell" owner:self options:nil] firstObject];
            }
        }
        cell.titleLable.text = self.firstArray[indexPath.row];

        //最终还是要隐藏
        cell.topLineView.hidden = YES;
        cell.bottomLineView.hidden = YES;
        return cell;
        
    }
    else
    {
        setSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"second"];
        if (cell == nil) {
            if (IPhone6Plus) {
                 cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"setSecondCell" owner:self options:nil] firstObject];
            }
        }
        cell.titleLable.text = self.secondArray[indexPath.row];
        /**
         *  根据self.switchStatesArray来设置switch的开关
         */
        if ([self.switchStatesArray[indexPath.row+1]intValue] == 0) {
            cell.isSwitch.on = NO;
        }else
        {
            cell.isSwitch.on = YES;
        }
        cell.isSwitch.tag = indexPath.row + 10;
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGFloat viewHeight = 46;
    CGFloat viewLeft = 15;
    CGFloat textfont = 14;
    if (IPhone6Plus) {
        viewHeight = 54;
        viewLeft = 20;
        textfont = 16;
    }
    if (section== 1) {
        
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, viewHeight)];
        view.backgroundColor = [UIColor clearColor];
        UILabel * sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(viewLeft,(viewHeight - 20)/2.0, kScreenWidth, 20)];
        sectionLabel.font = [UIFont systemFontOfSize:textfont];
        sectionLabel.text = @"在发现页面可以选择显示的以下内容";
        [view addSubview:sectionLabel];
        return view;
    }else{
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, viewHeight)];
        view.backgroundColor = [UIColor clearColor];
        UILabel * sectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(viewLeft, (viewHeight - 20)/2.0, kScreenWidth, 20)];
        sectionLabel.font = [UIFont systemFontOfSize:textfont];
        sectionLabel.text = @"在发现页面固定显示以下内容";
        [view addSubview:sectionLabel];
        return view;

    }
}- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
